import {Component, Input, OnInit, ViewChild, ViewContainerRef} from '@angular/core';
import {DhlCommonInputComponent} from '../dhlCommonInput';
import {DhlTableComponent} from '../table/dhl-table.component';
import {ObjectUtil} from '../../../utils/object-util';
import {TransferItem} from "ng-zorro-antd/transfer";

@Component({
  selector: 'dhl-transfer',
  templateUrl: './dhl-transfer.component.html'
})
export class DhlTransferComponent extends DhlCommonInputComponent implements OnInit {

  list: TransferItem[] = [];
  disabled = false;
  leftCheckRow = [];
  rightCheckRow = [];
  rightDatas = [];
  leftDisableDatas = [];
  leftNotDisabledData = [];
  checkedDatas = [];

  @Input() queryWidth = [[8, 8, 8]];
  @Input() primaryKey = 'id';
  @Input() fields;
  @Input() domain;
  @Input() params;
  @Input() showQuery = true;
  @Input() showSort = false;
  @Input() showAdd = false;
  @Input() formConfig = {};
  @ViewChild('renderLeft', {read: ViewContainerRef}) renderLeft: ViewContainerRef;
  @ViewChild('renderRight', {read: ViewContainerRef}) renderRight: ViewContainerRef;
  @ViewChild('leftTable', {read: DhlTableComponent}) leftTableRef: DhlTableComponent;
  @ViewChild('rightTable', {read: DhlTableComponent}) rightTableRef: DhlTableComponent;


  leftCheckedChange({checkedDatas}) {
    this.leftCheckRow = checkedDatas.filter(item => !item.disabled);
  }

  leftCheckAll(obj) {
    this.leftCheckRow = obj.filter(item => !item.disabled);
  }

  rightCheckedChange({checkedDatas}) {
    this.rightCheckRow = checkedDatas.filter(item => item.checked);
  }

  rightCheckAll(obj) {
    this.rightCheckRow = obj.filter(item => item.checked);
  }

  leftClick() {
    if (ObjectUtil.isHaveValue(this.rightCheckRow) && this.rightCheckRow.length > 0) {
      const newCheckRows = [];
      const newDisableRows = [];
      const ids = ',' + this.rightCheckRow.map(item => item[this.primaryKey]).reduce((x, y) => x + ',' + y) + ',';
      this.leftDisableDatas.forEach(item => {
        if (ids.indexOf(',' + item[this.primaryKey] + ',') < 0) {
          newDisableRows.push(item);
        }
      });
      this.rightDatas.forEach(item => {
        if (ids.indexOf(',' + item[this.primaryKey] + ',') < 0) {
          newCheckRows.push(item);
        }
      });


      this.rightDatas = newCheckRows;
      this.leftDisableDatas = newDisableRows;
      this.leftNotDisabledData = this.rightCheckRow;
      this.rightCheckRow = [];
      this.rightTableRef.clearCheckedData();
      this.inputValue = newCheckRows.map(item => item[this.primaryKey]);
    }
  }

  rightClick() {

    if (ObjectUtil.isHaveValue(this.leftCheckRow) && this.leftCheckRow.length > 0) {

      const newRightCheckRows = this.leftCheckRow.map(item => ({...item, 'checked': false}));
      this.rightDatas = [...this.rightDatas, ...newRightCheckRows];

      for (const item of this.leftCheckRow) {
        item.disabled = true;
      }
      this.leftDisableDatas = [...this.leftDisableDatas, ...this.leftCheckRow];
      this.leftCheckRow = [];
      this.inputValue = this.rightDatas.map(item => item[this.primaryKey]);
    }

  }

  upClick() {
    const length = this.rightCheckRow.length;
    const newSortData = [];
    if (length == 1) {
      for (const row of this.rightDatas) {
        if (newSortData.length > 0 && this.rightCheckRow[0][this.primaryKey] == row[this.primaryKey]) {
          const preRow = newSortData.pop();
          newSortData.push(row);
          newSortData.push(preRow);
        } else {
          newSortData.push(row);
        }
      }
      this.rightDatas = newSortData;
      this.inputValue = newSortData.map(item => item[this.primaryKey]);
    }
  }

  downClick() {
    const length = this.rightCheckRow.length;
    const newSortData = [];
    if (length == 1) {
      let preRow;
      for (const row of this.rightDatas) {
        if (preRow) {
          newSortData.push(row);
          newSortData.push(preRow);
          preRow = undefined;
        } else {
          if (newSortData.length != this.rightDatas.length - 1 && this.rightCheckRow[0][this.primaryKey] == row[this.primaryKey]) {
            preRow = row;
          } else {
            newSortData.push(row);
          }
        }
      }
      this.rightDatas = newSortData;
      this.inputValue = newSortData.map(item => item[this.primaryKey]);
    }
  }

  afterInitValue() {
    setTimeout(() => {

      if (ObjectUtil.isArray(this.value) && this.value.length > 0) {
        const key = this.primaryKey;
        this.checkedDatas = this.value.map(id => ({[key]: id, checked: true, disabled: true}));
        this.leftDisableDatas = this.checkedDatas;
        const paramsKey = this.primaryKey != 'id' ? this.primaryKey : 'filter_id_in_[i]';
        this.requestService.get(`${this.domain}/list`, {...this.params, [paramsKey]: this.value}).subscribe(data => {
          if (data) {
            this.rightDatas = data;
          }
        });
      }
    }, 50);
  }

  ngOnInit(): void {
    $('[dhl-input-element]', this.inputControl.nativeElement).attr('isArray', 'true');
  }


}






